﻿@using LiveChartsCore;
@using LiveChartsCore.SkiaSharpView;
@using LiveChartsCore.SkiaSharpView.Blazor
@using System.Collections.ObjectModel;
@inject NavigationManager NavigationManager
@inject IJSRuntime JS

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)"></LayoutView>
    </NotFound>
</Router>

@code {
    public record City(string Name, double Population);

    protected override void OnInitialized()
    {
        LiveCharts.Configure(config => // mark
            config // you can override the theme
                // .AddDarkTheme() // mark

                // finally register your own mappers
                // you can learn more about mappers at:
                // https://livecharts.dev/docs/{{ platform }}/{{ version }}/Overview.Mappers

                // here we use the index as X, and the population as Y // mark
                .HasMap<City>((city, index) => new(index, city.Population)) // mark
            // .HasMap<Foo>( .... ) // mark
            // .HasMap<Bar>( .... ) // mark
            ); // mark
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        RenderSidebar(NavigationManager.ToBaseRelativePath(NavigationManager.Uri));
        NavigationManager.LocationChanged += (object? sender, LocationChangedEventArgs e) =>
        {
            RenderSidebar(NavigationManager.ToBaseRelativePath(e.Location));
        };
    }

    private void RenderSidebar(string uri)
    {
         if (uri.ToLower().Contains("hello") || uri.Length == 0)
        {
            JS.InvokeVoidAsync("hideSideBar");
        }
        else
        {
            JS.InvokeVoidAsync("showSideBar");
        }
    }
}
